//
//  SVTabBarController.m
//  SilenceVoice
//
//  Created by 张诗宇 on 16/1/27.
//  Copyright © 2016年 张诗宇. All rights reserved.
//

#import "SVTabBarController.h"
#import "SVMPViewController.h"
#import "SVPlayButtonView.h"
#import "SVCustomButton.h"
#import "SVHomeViewController.h"
#import "SVMCViewController.h"
#import "SVExploreViewController.h"
#import "SVUserPageViewController.h"

@interface SVTabBarController ()

@property (strong ,nonatomic) SVPlayButtonView *playBtn;

@property (nonatomic, weak) SVCustomButton *previousBtn;

@end

@implementation SVTabBarController

// 重写设置tabbar是否隐藏
- (void)isHiddenTabBar:(BOOL)hidden withViewController:(UIViewController *)viewController {
    
    // 先删掉自己自定义的
    [self.tabBarImageView removeFromSuperview];
    
    if (hidden) {
        
        [self.tabBar setHidden:NO];
        [self.tabBarImageView setFrame:self.tabBar.frame];
        // 如果是隐藏就将自定义tabbar添加到系统的tabbar上
        [self.tabBar addSubview:self.tabBarImageView];
        if (viewController != nil) {
            viewController.hidesBottomBarWhenPushed = YES;
        }
        
    } else {
        [self.tabBar setHidden:YES];
        [self.tabBarImageView setFrame:self.tabBar.frame];
        [self.tabBar endEditing:NO];
        // 如果是显示就将自定义tabbar添加到view上
        [self.view addSubview:self.tabBarImageView];
        
    }
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    // 判断音乐
    
    //    发送图片消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeImage:) name:@"changeImage" object:nil];
    
    
    
    SVMPViewController *musicPlayer = [SVMPViewController sharePlayerViewController];
    
    if (musicPlayer.player.state == STKAudioPlayerStatePlaying) {
        
        [self.playBtn startCircling];
    }
    if (musicPlayer.player.state == STKAudioPlayerStatePaused) {
        
        [self.playBtn stopCircling];
        
    }
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBarImageView = [[UIImageView alloc] initWithFrame:self.tabBar.frame];
    self.tabBarImageView.userInteractionEnabled = YES;
    self.tabBarImageView.image = [UIImage imageNamed:@""];
    self.tabBarImageView.backgroundColor = [UIColor whiteColor];
    
    
    [self isHiddenTabBar:NO withViewController:nil];
    
    SVHomeViewController *homeVC = [[SVHomeViewController alloc] init];
    [self createButtonWithImageName:@"yinfu.png" setUpChildViewController:homeVC andSelectName:@"yinfu1.png" andTitle:@"热门推荐" andIndex:0];
    
    
    SVMCViewController *musicChannelVC = [[SVMCViewController alloc] init];
    [self createButtonWithImageName:@"pindao.png" setUpChildViewController:musicChannelVC andSelectName:@"pindao1.png"andTitle:@"音乐频道"andIndex:1];
    
    
    [self createButtonWithImageName:@"logo.png" setUpChildViewController:nil andSelectName:@"logo.png" andTitle:@"" andIndex:2];
    
    
    SVExploreViewController *exploreVC = [[SVExploreViewController alloc] init];
    [self createButtonWithImageName:@"pengyouquan.png" setUpChildViewController:exploreVC andSelectName:@"pengyouquan1.png"andTitle:@"探索活动"andIndex:3];
    
    SVUserPageViewController *userVC = [[SVUserPageViewController alloc] init];
    [self createButtonWithImageName:@"yinleren.png" setUpChildViewController:userVC andSelectName:@"yinleren1.png"andTitle:@"我的"andIndex:4];
    
    
    //  默认选中第一个按钮
    SVCustomButton *btn = self.tabBarImageView.subviews[0];
    
    [self changeViewController:btn]; //自定义的控件中得按钮被点击了调用的方法，默认进入界面就选中第一个按钮
    
    
    
    
    CGFloat buttonW = self.tabBarImageView.frame.size.width / 5;
    
    CGFloat buttonH = self.tabBarImageView.frame.size.height;
    
    self.playBtn = [[SVPlayButtonView alloc] initWithFrame:CGRectMake(buttonW * 2, 5, buttonW, buttonH)];
    
    
    
    [self.playBtn.musicButton setImage:[UIImage imageNamed:@"right"] forState:UIControlStateNormal];
    
    [self.tabBarImageView addSubview:self.playBtn];
    
    [self.playBtn.musicButton addTarget:self action:@selector(playBtnAction:) forControlEvents:UIControlEventTouchUpInside];

    
}

#pragma mark 按钮被点击时调用

- (void)changeViewController:(SVCustomButton *)sender {
    
    //*****关键,设置选中的控制器
    self.selectedIndex = sender.tag ; //切换不同控制器的界面
    NSLog(@"%ld", sender.tag);
    sender.enabled = NO ;//让选中的按钮变为不可用,也就是限制点击一次
    
    //如果当前点击的按钮不是保存的按钮.那么将按钮设置为可用状态.
    //也就是两次点击的不是同一个按钮
    if (self.previousBtn != sender) {
        
        self.previousBtn.enabled = YES ;
        
    }
    //记录当前点击的按钮
    self.previousBtn = sender;
    
}

-(void)playBtnAction:(UIButton *)btn
{
    
    SVMPViewController *musicPlayer = [SVMPViewController sharePlayerViewController];
    
    [self presentViewController:musicPlayer animated:YES completion:^{
        
        
    }];
    
}

#pragma mark 创建按钮通用方法
- (void)createButtonWithImageName:(NSString *)image setUpChildViewController:(UIViewController *)controller andSelectName:(NSString *)selected andTitle:(NSString *)title andIndex:(int)index {
    SVCustomButton *button = [SVCustomButton buttonWithType:UIButtonTypeCustom];
    
    button.tag = index;
    
    CGFloat buttonW = self.tabBarImageView.frame.size.width / 5;
    
    CGFloat buttonH = self.tabBarImageView.frame.size.height;
    button.frame = CGRectMake(buttonW * index, 5, buttonW, buttonH);
    
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    
    [button setImage:[UIImage imageNamed:selected] forState : UIControlStateDisabled];//设置按钮无效时的图片样式
    
    [button setTitle:title forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor colorWithRed:0.149 green:0.212 blue:0.298 alpha:1.000]forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(changeViewController:) forControlEvents: UIControlEventTouchDown];
    
    button.imageView.contentMode = UIViewContentModeScaleAspectFit; // 让图片在按钮内居中
    
    button.titleLabel.textAlignment = NSTextAlignmentCenter ; // 让标题在按钮内居中
    
    button.titleLabel.font = [UIFont systemFontOfSize:10]; // 设置标题的字体大小
    
    [self.tabBarImageView addSubview:button];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
    [self addChildViewController:nav];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
